﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Resources;

namespace TheOliver.Controls
{
    public partial class Book : UserControl
    {
        private PageTurn _ptf;

        private string[] _captions =
        {
            "BVM BobCat",
            "BVM Bandit",
            "KMP B-25 Mitchell",
            "Top-Flite F4U Corsair (Gold Edition)",
            "Hangar 9 P-40 Warhawk"
        };
        
        public Book()
        {
            InitializeComponent();

            // Register a handler for the control's Resized events
            Application.Current.Host.Content.Resized += new EventHandler(OnControlResized);
            
            // Begin downloading Pages.zip
            WebClient wc = new WebClient();
            wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged);
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(OnOpenReadCompleted);
            wc.OpenReadAsync(new Uri("Pages.zip", UriKind.Relative));
        }

        private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Progress.Value = e.ProgressPercentage;
        }

        private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            Progress.Visibility = Visibility.Collapsed;

            if (e.Error == null)
            {
                StreamResourceInfo sri1 = new StreamResourceInfo(e.Result, null);

                // Load the images from Pages.zip
                BitmapImage image1 = new BitmapImage();
                image1.SetSource(Application.GetResourceStream(sri1, new Uri("BobCat-a.jpg", UriKind.Relative)).Stream);
                Image0a.Source = image1;

                BitmapImage image2 = new BitmapImage();
                image2.SetSource(Application.GetResourceStream(sri1, new Uri("BobCat-b.jpg", UriKind.Relative)).Stream);
                Image0b.Source = image2;

                BitmapImage image3 = new BitmapImage();
                image3.SetSource(Application.GetResourceStream(sri1, new Uri("Bandit-a.jpg", UriKind.Relative)).Stream);
                Image1a.Source = image3;

                BitmapImage image4 = new BitmapImage();
                image4.SetSource(Application.GetResourceStream(sri1, new Uri("Bandit-b.jpg", UriKind.Relative)).Stream);
                Image1b.Source = image4;

                BitmapImage image5 = new BitmapImage();
                image5.SetSource(Application.GetResourceStream(sri1, new Uri("B-25a.jpg", UriKind.Relative)).Stream);
                Image2a.Source = image5;

                BitmapImage image6 = new BitmapImage();
                image6.SetSource(Application.GetResourceStream(sri1, new Uri("B-25b.jpg", UriKind.Relative)).Stream);
                Image2b.Source = image6;

                BitmapImage image7 = new BitmapImage();
                image7.SetSource(Application.GetResourceStream(sri1, new Uri("Corsair-a.jpg", UriKind.Relative)).Stream);
                Image3a.Source = image7;

                BitmapImage image8 = new BitmapImage();
                image8.SetSource(Application.GetResourceStream(sri1, new Uri("Corsair-b.jpg", UriKind.Relative)).Stream);
                Image3b.Source = image8;

                BitmapImage image9 = new BitmapImage();
                image9.SetSource(Application.GetResourceStream(sri1, new Uri("P-40a.jpg", UriKind.Relative)).Stream);
                Image4a.Source = image9;

                BitmapImage image10 = new BitmapImage();
                image10.SetSource(Application.GetResourceStream(sri1, new Uri("P-40b.jpg", UriKind.Relative)).Stream);
                Image4b.Source = image10;

                // Initialize the page-turn framework
                _ptf = new PageTurn();

                _ptf.AddSpread(Spread0a, Spread0b);
                _ptf.AddSpread(Spread1a, Spread1b);
                _ptf.AddSpread(Spread2a, Spread2b);
                _ptf.AddSpread(Spread3a, Spread3b);
                _ptf.AddSpread(Spread4a, Spread4b);

                _ptf.Initialize(PageTurnCanvas);
                _ptf.Sensitivity = 1.2;

                // Initialize the caption bar
                Caption.Text = _captions[_ptf.CurrentSpreadIndex];

                // Display the page-turn canvas
                PageTurnCanvas.Visibility = Visibility.Visible;
                
                // Register a PageTurned event handler for updating the caption bar
                _ptf.PageTurned += new EventHandler(OnPageTurned);
            }
        }

        private void OnPageTurned(Object sender, EventArgs e)
        {
            // Update the caption bar
            Caption.Text = _captions[_ptf.CurrentSpreadIndex];
        }

        private void OnControlResized(Object sender, EventArgs e)
        {
            // Resize the main canvas to fit the width of the control
            ZoomTransform.ScaleX = ZoomTransform.ScaleY = (Application.Current.Host.Content.ActualWidth / PageTurnCanvas.Width) * 0.95;
        }
    }
}
